package owen.com.dxc;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Timer;
import java.util.TimerTask;

import owen.com.dxc.db.DBHelper;

public class Login extends AppCompatActivity {

    private EditText nameEt,passwordEt;
    private Button loginBtn;
    private TextView tv;
    private DBHelper dbHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
        init();
        dbHelper = new DBHelper(this);
    }

    public void init(){
        nameEt = (EditText)findViewById(R.id.name);
        passwordEt = (EditText)findViewById(R.id.password);

        loginBtn = (Button)findViewById(R.id.loginBtn);
        loginBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String msg = checkLogin();
                if("success".equals(msg)){
                    Toast.makeText(Login.this, "登录成功", Toast.LENGTH_SHORT).show();

                    Timer timer = new Timer();
                    timer.schedule(new TimerTask() {
                        @Override
                        public void run() {
                            Intent intent = new Intent(Login.this, Index.class);
                            intent.putExtra("name", nameEt.getText().toString().trim());
                            startActivity(intent);
                        }
                    }, 1000);
                }else {
                    Toast.makeText(Login.this, msg, Toast.LENGTH_SHORT).show();
                }
            }
        });

        tv = (TextView)findViewById(R.id.toRegisterTv);
        tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(Login.this, Register.class));
            }
        });
    }

    public String checkLogin() {
        String name = nameEt.getText().toString().trim();
        String password = passwordEt.getText().toString().trim();

        if(name.length() == 0){
            return "你是sei？你是不是偷井盖的贼";
        }else {
            SQLiteDatabase db = dbHelper.getWritableDatabase();
            Cursor cursor = db.rawQuery("select * from user where name = ?", new String[]{name});
            if(cursor.getCount() < 1){
                cursor.close();
                return "暂查无此人";
            }else {
                if(password.length() == 0){
                    return "密码呢？拉去喂狗";
                }else {
                    while (cursor.moveToNext()){
                        String dbPassword = cursor.getString(cursor.getColumnIndex("password"));
                        if(!dbPassword.equals(password)){
                            cursor.close();
                            return "密码错误";
                        }
                    }
                }
            }
            cursor.close();
        }

        return "success";
    }

    //退出应用
    private long firstTime = 0;
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN){
            long currentTime = System.currentTimeMillis();
            if(currentTime-firstTime > 2000){
                Toast.makeText(Login.this, "再按一次退出", Toast.LENGTH_SHORT).show();
                firstTime = currentTime;
            }else{
                finish();
                System.exit(0);
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
